﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Coolite.Ext.Web;
using Coolite.Ext.UX;
using Sagacity.NC.Business.SysMange;
using System.Collections.Generic;

namespace Sagacity.NC.Web.BasicSettings.Student
{
    public partial class StuZaiXiaoInfo : PageBase
    {
        Sagacity.NC.Business.SysMange.Common _common = new Sagacity.NC.Business.SysMange.Common();
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadTreeOrg();
        }

        protected override void OnInitComplete(EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadRegionTree();
            }
            base.OnInitComplete(e);
        }

        protected void LoadRegionTree()
        {
            ComboTree cbTree = new ComboTree();
            cbTree.ID = "ComboTree1";
            cbTree.EmptyText = "—请选择区域—";
            cbTree.Resizable = true;
            cbTree.Width = 170;
            //cbTree.Listeners.Blur.Handler = "javascript:SalaryTypeSelected();";


            TreePanel tree = new TreePanel();
            tree.ID = "TreePanel1";
            tree.Header = false;
            tree.AutoHeight = true;
            tree.Border = false;
            tree.RootVisible = false;

            Coolite.Ext.Web.TreeNode root = new Coolite.Ext.Web.TreeNode("RootNode");

            DataTable dtRegion = Sagacity.NC.Business.SysMange.Common.GetRoleRightData(CurrentUser.LoginRole, int.Parse(hidRegionResourceId.Value));
            System.Data.DataView dtveiw = new System.Data.DataView(dtRegion);
            dtveiw.Sort = hidRegionParentColumn.Value;
            dtRegion = dtveiw.ToTable();

            AddNodes(root, int.Parse(dtRegion.Rows[0][hidRegionParentColumn.Value].ToString()), dtRegion);
            tree.Root.Add(root);

            cbTree.Tree.Add(tree);

            List<object> list = new List<object>();

            foreach (DataRow r in dtRegion.Rows)
            {
                list.Add(new { text = r[hidRegionCaptionColumn.Value].ToString(), value = r[hidRegionColumn.Value].ToString() });
            }

            cbTreeStore.DataSource = list;
            cbTreeStore.DataBind();

            cbTree.StoreID = "cbTreeStore";

            TreePanel treePanel = TreeFormCommon1.FindControl(hidtoolBarPanelId.Value) as TreePanel;
            Toolbar toolbar = treePanel.FindControl("toolBarPanel") as Toolbar;
            toolbar.Items.Add(cbTree);
        }

        private void AddNodes(Coolite.Ext.Web.TreeNode node, int parentId, DataTable dt)
        {
            DataRow[] nodeDatas = dt.Select(hidRegionParentColumn.Value + "=" + parentId);
            foreach (DataRow r in nodeDatas)
            {
                Coolite.Ext.Web.TreeNode childNode = new Coolite.Ext.Web.TreeNode();
                childNode.Listeners.Click.Handler = "javascript:refreshSchoolInfo(this);";
                childNode.Text = r[hidRegionCaptionColumn.Value].ToString();
                childNode.NodeID = r[hidRegionColumn.Value].ToString();
                AddNodes(childNode, int.Parse(childNode.NodeID), dt);
                node.Nodes.Add(childNode);
            }
        }

        /// <summary>
        /// 加载组织机构树
        /// </summary>
        protected void LoadTreeOrg()
        {
            Hashtable hashtable = new Hashtable();
            hashtable.Add("keyId", hidSchoolIDColumn.Value);
            hashtable.Add("keyName", hidSchoolCaptionColumn.Value);
            hashtable.Add("keyParent", hidSchoolParentColumn.Value);

            if(!IsPostBack)
                this.TreeFormCommon1.Filters.Add(new Sagacity.Entity.Query.FilterInfo("Region_ID","=",0));

            this.TreeFormCommon1.AddFieldS = hashtable;
            this.TreeFormCommon1.CurrentUser = CurrentUser;
            this.TreeFormCommon1.Role = CurrentUser.LoginRole;
            string[] PrimaryKeyColumns = new string[] { hidSchoolParentColumn.Value, hidSchoolIDColumn.Value, hidSchoolCaptionColumn.Value };
            this.TreeFormCommon1.arrayString = PrimaryKeyColumns;
            this.TreeFormCommon1.title = "区域学校";
            this.TreeFormCommon1.toolstarte = false;
            this.TreeFormCommon1.TableName = this.hidTableName.Value.ToString();
        }
    }
}
